home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / source / src / graph / _g_inout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-16  |  3.4 KB  |  174 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  _g_inout.c
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16. #include <LEDA/graph.h>
  17. #include <LEDA/stream.h>
  18.  
  19.  
  20. //--------------------------------------------------------------------------
  21. // graph i/o
  22. //--------------------------------------------------------------------------
  23.  
  24.  
  25. void put_int(filebuf& fb, int n)
  26. { register char* A = (char*)&n;
  27.            char* E = A+4;
  28.  
  29.   while (A<E) fb.sputc(*(A++));
  30. }
  31.  
  32. int get_int(istream& from)
  33. { int n;
  34.   register char* A = (char*)&n;
  35.            char* E = A+4;
  36.   while (A<E) from.get(*(A++));
  37.   return n;
  38. }
  39.  
  40. void graph::write(string file_name) const
  41. { char* s = ~file_name;
  42.   file_ostream out(s);
  43.   if (out.fail()) error_handler(1,"write: cannot open file");
  44.   write(out);
  45. }
  46.  
  47.  
  48. void graph::write(ostream& out) const
  49. {
  50.   int* A = new int[max_n_index+2];
  51.  
  52.   // nodes get numbers from 1 to |V| (trouble with 0)
  53.  
  54.   int count = 1;
  55.  
  56.   out << "LEDA.GRAPH\n";
  57.   out << node_type() << "\n";
  58.   out << edge_type() << "\n";
  59.  
  60.   out << V.length() << "\n";
  61.  
  62.   node v;
  63.   forall_nodes(v,*this)
  64.   { write_node_entry(out,v->data[0]);
  65.     out << "\n";
  66.     A[v->name] = count++;
  67.    }
  68.  
  69.   out << E.length() << "\n";
  70.   edge e;
  71.   forall_edges(e,*this)
  72.   { out << A[e->s->name] << " " << A[e->t->name] << " ";
  73.     write_edge_entry(out,e->data[0]);
  74.     out << "\n";
  75.    }
  76. delete A;
  77. }
  78.  
  79. int graph::read(string file_name)
  80. { char* s = ~file_name;
  81.   file_istream in(s);
  82.   if (in.fail())  return 1;
  83.   return read(in);
  84. }
  85.  
  86. int graph::read(istream& in)
  87. { int result = 0;
  88.  
  89.   clear();
  90.  
  91.   edge e;
  92.   int n,i,v,w;
  93.  
  94.   string d_type,n_type,e_type;
  95.  
  96.   string this_n_type = node_type();
  97.   string this_e_type = edge_type();
  98.  
  99.   while (in && d_type=="") in >> d_type;
  100.  
  101.   in >> n_type >> e_type >> n;
  102.  
  103.   if (d_type != "LEDA.GRAPH") return 3;
  104.  
  105.   read_line(in);
  106.  
  107.   node* A = new node[n+1];
  108.  
  109.   if (n_type != this_n_type)
  110.   { if (this_n_type != "void") result = 2;   // incompatible node types
  111.     for (i=1; i<=n; i++)
  112.     { A[i] = new_node();
  113.       read_line(in);
  114.      }
  115.    }
  116.   else
  117.     for (i=1; i<=n; i++)
  118.     { A[i] = new_node(0);
  119.       read_node_entry(in,A[i]->data[0]);
  120.      }
  121.  
  122.   in >> n;       // number of edges
  123.  
  124.   if (e_type != this_e_type)
  125.   { if (this_e_type != "void") result = 2;   // incompatible edge types
  126.     while (n--) { in >> v >> w;
  127.                   e = new_edge(A[v],A[w]);
  128.                   read_line(in);
  129.                  }
  130.    }
  131.   else
  132.    while (n--) { in >> v >> w;
  133.                  e = new_edge(A[v],A[w],0);
  134.                  read_edge_entry(in,e->data[0]);
  135.                 }
  136.  
  137.   delete A;
  138.   return result;
  139. }
  140.  
  141.  
  142. void graph::print_node(node v,ostream& o) const
  143. { if (super() != 0)
  144.      super()->print_node(node(graph::inf(v)),o);
  145.   else
  146.      { o << "[" << index(v) <<"]" ;
  147.        print_node_entry(o,v->data[0]);
  148.       }
  149. }
  150.  
  151. void graph::print_edge(edge e,ostream& o) const
  152. { if (super() != 0)
  153.      super()->print_edge(edge(graph::inf(e)),o);
  154.   else
  155.      { o <<   "[" << e->s->name << "]--";
  156.        print_edge_entry(o,e->data[0]);
  157.        o << "-->[" << e->t->name << "]";
  158.       }
  159. }
  160.  
  161. void graph::print(string s, ostream& out) const
  162. { node v;
  163.   edge e;
  164.   out << s << endl;
  165.   forall_nodes(v,*this)
  166.   { print_node(v,out);
  167.     out << " : ";
  168.     forall_adj_edges(e,v) print_edge(e,out);
  169.     out << endl;
  170.    }
  171.   out << endl;
  172. }
  173.